package com.controllers
{
	import mx.collections.ArrayCollection;
	import com.data.Score;
	
	public class ParetoRanking
	{
		private var scores:ArrayCollection; // holds Score objects
		
		public static const FRONT:int = -1;
		public static const NONFRONT:int = -2;
		
		public function ParetoRanking(_scores:ArrayCollection)
		{
			this.scores = _scores;
			
		}
		
		private function dominates(ind1:Object, ind2:Object):Boolean
		{
			var dominated:Boolean = false;
			for each(var score:Score in this.scores)
			{
				if (score.isMaximizing()) {
					if (ind1[score.name] < ind2[score.name])
						return false;
				} else {
					if (ind1[score.name] > ind2[score.name])
						return false;
				}	
		 		if (ind1[score.name] != ind2[score.name])
					dominated = true;
			}
			return dominated;
		}
		
		public function findFront(inds:ArrayCollection): Array // returns a new array with boolean flags
		{
			var res:Array = new Array(inds.length);
			var i:int;
			
			for (i = 0; i < inds.length; i++)
				res[i] = FRONT;
			
			for (i = 0; i < inds.length; i++)
			{
				if (res[i] == FRONT)
					for (var j:int = 0; j < inds.length; j++)
					{
						if (dominates(inds[j], inds[i]))
							res[i] = NONFRONT;
						if (dominates(inds[i], inds[j]))
							res[j] = NONFRONT;	
					}	
				
				if (i%100 == 0)
					trace ("Ind " + i);
				
				//trace("Individual i is " + res[i]); 
			}
			
			trace("I have finished finding pareto front");
			return res;
		}
		
	}
}